'''
Created on Jul 20, 2013

@author: Cihat Basol
'''


def EventFromJSON(event_json):
    if (not event_json) or ("name" not in event_json):
        return None
    if event_json['name'] == JobStartedEvent.__name__:
        return JobStartedEvent()
    if event_json['name'] == JobStoppedEvent.__name__:
        return JobStoppedEvent()
    if event_json['name'] == PeriodicTimeEvent.__name__:
        return PeriodicTimeEvent(float(event_json['period']))
    if event_json['name'] == OneShotTimeEvent.__name__:
        return OneShotTimeEvent(float(event_json["after"]))
    return DexenEvent()


class DexenEvent(object):
    def __init__(self):
        pass


class JobStoppedEvent(DexenEvent):
    def __init__(self):
        super(JobStoppedEvent, self).__init__()

    def json(self):
        return {"name" : self.__class__.__name__}


class JobStartedEvent(DexenEvent):
    def __init__(self):
        super(JobStartedEvent, self).__init__()

    def json(self):
        return {"name" : self.__class__.__name__}


class TimeBasedEvent(DexenEvent):
    def __init__(self):
        super(TimeBasedEvent, self).__init__()


class PeriodicTimeEvent(TimeBasedEvent):
    def __init__(self, period):
        super(PeriodicTimeEvent, self).__init__()
        self.period = period # in seconds

    def json(self):
        return {"name" : self.__class__.__name__,
                "period" : self.period}


class OneShotTimeEvent(TimeBasedEvent):
    def __init__(self, after):
        super(OneShotTimeEvent, self).__init__()
        self.after = after

    def json(self):
        return {"name" : self.__class__.__name__,
                "after" : self.after}


